-- Debug
-- 调试界面
Debug = class()

function Debug:init(screenmode)
    self.textsize = 15
    self.fps = 0
    self.screen = screenmode
    self.tps = TouchPoints()
end

function Debug:draw()
    self.fps = math.floor((self.fps * 0.9 + 0.1 / DeltaTime) * 100) / 100
    if not SettingList.ShowDebug then
        return
    end
    fontSize(self.textsize)
    textMode(CORNER)
    self:textDebug("PhysicsBox " .. Version, 10, HEIGHT - ((self.textsize + 10) * 1))
    self:textDebug(LP.objectcount.. game.world:getObjectCount(), 10, HEIGHT - ((self.textsize + 10) * 2))
    self:textDebug("FPS:".. self.fps, 10, HEIGHT - ((self.textsize + 10) * 3))
    self:textDebug(LP.names..game:getFileName(),10,HEIGHT - ((self.textsize + 10) * 4))
    pushStyle()
    fill(0, 0, 0, 255)
    fontSize(30)
    textMode(CORNER)
    local bar = game.gui.bar
    if bar:getCurrentItem() then
        if LP.objectsname[bar:getCurrentItem().id] then
            text(LP.objectsname[bar:getCurrentItem().id],10, HEIGHT - ((self.textsize + 18) * 4))
        else
            text(bar:getCurrentItem().name,10, HEIGHT - ((self.textsize + 18) * 4))
        end
    end
    popStyle()
    
    pushStyle()
    fill(0, 0, 0, 255)
    fontSize(15)
    textMode(CENTER)
    text(os.date("%X",os.time()),WIDTH/2,HEIGHT - 14)
    popStyle()
    if self.screen == true then
        self.tps:draw()
    end
end

function Debug:textDebug(name, posX, posY)
    pushStyle()
    textMode()
    font("HelveticaNeue-Light")
    stroke(0, 0, 0, 255)
    fill(0, 0, 0, 255)
    text(name, posX, posY)
    popStyle()
end

function Debug:touched(touch)
    if self.screen == true then
        tps:touched(touch)
    end   
end

function Debug:drawCollisionBox(body)
    pushMatrix()
    if body.shapeType == CIRCLE then
        noFill()
        strokeWidth(2)
        stroke(Colour.Red)
        ellipse(body.x,body.y,body.radius)
    elseif body.shapeType == POLYGON then
        strokeWidth(2)
        stroke(Colour.Blue)
        translate(body.x,body.y)
        rotate(body.angle)
        local p1,p2
        for i = 1,#body.points-1 do
            p1 = body.points[i]
            p2 = body.points[i + 1]
            line(p1.x,p1.y,p2.x,p2.y)
        end
        p1 = body.points[1]
        p2 = body.points[#body.points]
        line(p1.x,p1.y,p2.x,p2.y)
    end
    popMatrix()
end


